home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
L' Effet Pommier 3
/
L'Effet Pommier - Volume 03.iso
/
Programmation
/
gray image 2.1
/
write_pgm.cc
< prev
next >
Wrap
Text File
|
1995-05-03
|
2KB
|
63 lines
// This may look like C code, but it is really -*- C++ -*-
/*
************************************************************************
*
* Grayscale Image
*
* Write the image into the file in the Portable GrayMap (pgm) format
*
* The program writes a "binary" (RAWBITS) pgm file of the following format
* - A "magic number" for identifying the file type. A pgm
* file's RAWBITS magic number is two characters "P5".
* - Whitespace (blanks, TABs, CRs, LFs).
* - A width, formatted as ASCII characters in decimal.
* - Whitespace.
* - A height, again in ASCII decimal.
* - Whitespace.
* - The maximum gray value, again in ASCII decimal. For RAWBITS pgm file
* the maximum grayscale value cannot exceed 255.
* - A _single_ character of whitespace (typically a newline).
* - Width * height gray values, each as plain bytes, between
* 0 and the specified maximum value, stored consecutivly,
* starting at the top-left corner of the graymap, proceding in normal
* English reading order.
* A value of 0 means black, and the maximum value means white.
*
* For more detail, see documentation on PBMPLUS package (specifically,
* pgm(5)).
*
* $Id: write_pgm.cc,v 2.0 1995/03/06 20:58:24 oleg Exp oleg $
*
************************************************************************
*/
#include "image.h"
#include "endian_io.h"
void IMAGE::write_pgm(const char * file_name,const char * title) const
{
is_valid();
message("\nPreparing a PGM file with name '%s'\n",file_name);
assure(bits_per_pixel <= 8,
"Sorry, the program cannot write images with depth > 8");
const card max_gray_value = (1<<bits_per_pixel)-1;
EndianOut outf(file_name);
assert( outf.good() );
// Write the header
outf << "P5\n" << ncols << ' ' << nrows << '\n' << max_gray_value << '\n';
class WritePixelMatrix : public PixelPrimAction
{
EndianOut& outf;
void operation(GRAY& pixel) { outf.write_byte(pixel); }
public: WritePixelMatrix(EndianOut& ostream) : outf(ostream) {}
};
(*(IMAGE*)this).apply(WritePixelMatrix(outf));
outf.close();
}